﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PaceR.Data.Context;
using PaceR.Core.EntityStorage;

namespace PaceR.Test.Data.Context
{
    [TestClass]
    public class EntityStoreContextTest
    {
        [TestMethod]
        public void EntityStoreTest()
        {
            //create test context
            var context = new EntityStoreContext(new SimpleStore());
            Assert.AreEqual(0, context.Find<string>().Count());

            string t1 = "test1";

            context.Save(t1);
            Assert.AreEqual(0, context.Find<string>().Count());

            context.Commit();
            Assert.AreEqual(1, context.Find<string>().Count());

            var result = context.FirstOrDefault<string>(s => s == t1);
            Assert.IsNotNull(result);
            Assert.AreEqual(t1, result);

            string t2 = "test2";

            context.Save(t2);
            context.Remove(result);
            Assert.AreEqual(1, context.Find<string>().Count());   
            Assert.IsNotNull(context.First<string>(t => t == t1));

            context.Commit();
            Assert.AreEqual(1, context.Find<string>().Count());
            Assert.AreEqual(t2, context.Find<string>().FirstOrDefault());

            context.RemoveResults<string>(c => c.Where(s => s == t2));
            Assert.AreEqual(1, context.Find<string>().Count());

            context.Commit();
            Assert.AreEqual(0, context.Find<string>().Count());
            Assert.IsNull(context.FirstOrDefault<string>(s => s == t2));
        }
    }
}
